import { NestMiddleware } from "@nestjs/common";
import { w7 } from '@sker/core'
import { CoreError, getErrorsMsg, } from "@sker/core";
export class SkerNestMiddleware implements NestMiddleware {
    use(req: any, res: any, next: (error?: any) => void) {
        w7(req, res, (error) => {
            if (error) {
                if (error instanceof CoreError) {
                    res.status(error.code).json(error.toJson())
                    return;
                }
                if (Reflect.has(error, 'errors')) {
                    const messages = getErrorsMsg(error)
                    res.json({ success: false, message: messages, errors: Reflect.get(error, 'errors') || [] })
                    return;
                }
                if (Reflect.has(error, 'toJson')) {
                    res.json((error as any).toJson())
                    return;
                }
                const code = Reflect.get(error, 'code')
                res.json({ success: false, message: error.message, data: { code: code, stack: error.stack } });
            } else {
                next();
            }
        })
    }
}
